<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

session_start();

ob_start();

require_once dirname(__FILE__).'/Debug.php';
require_once dirname(__FILE__).'/Component.php';
require_once dirname(__FILE__).'/Application/Router.php';
require_once dirname(__FILE__).'/Application/Controller/CustomController.php';
require_once dirname(__FILE__).'/Application/Sorter.php';
require_once dirname(__FILE__).'/Application/View.php';
require_once dirname(__FILE__).'/Application/Exception.php';
require_once dirname(__FILE__).'/Application/Config.php';
require_once dirname(__FILE__).'/Library/Cache.php';

class Application
{ 

    /**
     * Dir application
     * @var string
     */
    public static  $appDir;


    /**
     * Router
     * @var object
     */
    public         $Router;


    /**
     * Http
     * @var object
     */
    public         $Http;


    /**
     * Application
     * @var object
     */
    private static  $self;



    /**
     * Load Application
     * @param string $application
     */
    public function __construct($application)
    {
        self::$self   = $this;
        self::$appDir = $application;
        Cache::$dir   = $application.'/cache/';
        Debug::run();

        $component = new Component(dirname(__FILE__).'/Library/');
        $component->register();

        $this->Router = new Router();
        $this->Http   = new Http();
        $this->Config = new Config();
    }



    /**
     * Return application from static instance
     * @return object
     */
    public static function get()
    {
        return self::$self;
    }



    /**
     * Load class
     * @param string $type
     * @param string $name
     */
    public function loadClass($type, $name, $exceptionCode = 0)
    {
          $file = call_user_func_array(array('Sorter','get'.$type.'File'),array($name, $this->Router->namespace));
          $file = (array) $file;
          foreach($file as $path){
               if(file_exists(self::$appDir.$path)){
                  require_once self::$appDir.$path;
                  return true;
               }
          }
          throw new ApplicationException(strtolower('missing-' .$type. '-' .$name), $exceptionCode);
    }



    /**
     * Run application
     */
    public function run()
    {
        try{
            $name = $this->Router->controller;
            $this->loadClass('Controller', $name);
            $name = Sorter::getController($name, true);
            $controller = new $name;
            $controller->render();

        }catch(ApplicationException $exception){
            if((int)$this->Config->debug > 0){
               $exception->renderViewException();
               
            }else{
                echo "Internal error";
            }
        }
    }
} 


?>